package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.ChiTietHoaDonReportDao;
import com.tt.fashionWebSite.dao.SanPhamFullDao;
import com.tt.fashionWebSite.dto.*;
import com.tt.fashionWebSite.service.GenericService;
import com.tt.fashionWebSite.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by isoft-work 5 on 1/15/2015.
 */
@Service("reportService")
public class ReportServiceImp extends GenericService implements ReportService {
   @Autowired
   @Qualifier("chiTietHoaDonReport")
   private ChiTietHoaDonReportDao chiTietHoaDonReportDao;

   @Override
   @Transactional(propagation = Propagation.REQUIRED)
   public List<RemainProductReportDto> getNumberRemainProduct() {
      // show follow each product
      List<RemainProductReportDto> remainProductReports = new ArrayList<RemainProductReportDto>();
      List<Object[]> remains = chiTietHoaDonReportDao.getNumberRemainProduct();
      for(Object[] remain : remains ) {
         RemainProductReportDto remainProductReportDto = new RemainProductReportDto(remain);
         remainProductReports.add(remainProductReportDto);
      }

      return remainProductReports;
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED)
   public List<RevenueDateAndSellMonthReportDto> getRevenueForDate(String date) {
      // show follow each product
      List<RevenueDateAndSellMonthReportDto> revenueDateAndSellMonthReports = new ArrayList<RevenueDateAndSellMonthReportDto>();
      List<Object[]> revenues = chiTietHoaDonReportDao.getRevenueForDate(date);
      for(Object[] revenue : revenues ) {
         RevenueDateAndSellMonthReportDto revenueDateAndSellMonthReportDto = new RevenueDateAndSellMonthReportDto(revenue);
         revenueDateAndSellMonthReports.add(revenueDateAndSellMonthReportDto);
      }
      return revenueDateAndSellMonthReports;
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED)
   public List<RevenueDateAndSellMonthReportDto> getNumberSellProductForMonth(String date) {
//      show follow each product
      List<String> result = new ArrayList<String>();
      DateFormat inputDF  = new SimpleDateFormat("yyyy-MM-dd");
      Integer month = 0; Integer year = 0 ;
      try {
         Date date1 = inputDF.parse(date);
         Calendar cal = Calendar.getInstance();
         cal.setTime(date1);
         month = cal.get(Calendar.MONTH) + 1;
         year = cal.get(Calendar.YEAR);
      }
      catch (Exception ex) {

      }

      List<RevenueDateAndSellMonthReportDto> revenueDateAndSellMonthReports = new ArrayList<RevenueDateAndSellMonthReportDto>();
      List<Object[]> SellProducts = chiTietHoaDonReportDao.getNumberSellProductForMonth(month.toString(), year.toString());
      for(Object[] SellProduct : SellProducts ) {
         RevenueDateAndSellMonthReportDto revenueDateAndSellMonthReportDto = new RevenueDateAndSellMonthReportDto(SellProduct);
         revenueDateAndSellMonthReports.add(revenueDateAndSellMonthReportDto);
      }
      return revenueDateAndSellMonthReports;
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED)
   public List<SaleProductReportDto> getNumberSaleProduct() {
      List<SaleProductReportDto> saleProductReports = new ArrayList<SaleProductReportDto>();
      List<Object[]> saleProducts = chiTietHoaDonReportDao.getNumberSaleProduct();
      for(Object[] saleProduct : saleProducts ) {
         SaleProductReportDto saleProductReportDto = new SaleProductReportDto(saleProduct);
         saleProductReports.add(saleProductReportDto);
      }
      return saleProductReports;
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED)
   public List<RevenueMonthReportDto> getRevenueForMonth(String date){
      // show follow each date
      List<String> allDay = new ArrayList<String>();
      List<RevenueMonthReportDto> revenueMonthReports = new ArrayList<RevenueMonthReportDto>();
      try {
         allDay = getAllDateInMonth(date);
      }
      catch (Exception ex) {

      }

      for(int i = 0; i < allDay.size(); i++) {
         List<Object[]> revenueMonths = chiTietHoaDonReportDao.getRevenueForMonth(allDay.get(i));
         for (Object[] revenueMonth: revenueMonths) {
            if(revenueMonth[0] != null) {
               RevenueMonthReportDto revenueMonthReportDto = new RevenueMonthReportDto(revenueMonth);
               revenueMonthReports.add(revenueMonthReportDto);
            }
            else {
               RevenueMonthReportDto revenueMonthReportDto = new RevenueMonthReportDto(allDay.get(i), 0.0);
               revenueMonthReports.add(revenueMonthReportDto);
            }
         }

      }

      return revenueMonthReports;
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED)
   public List<RevenueQuarterlyReportDto> getRevenueForQuarterly(String date) {
      // show follow each quarter
      List<String> result = new ArrayList<String>();
      DateFormat inputDF  = new SimpleDateFormat("yyyy-MM-dd");
      Integer month = 0, year = 0 ;
      try {
         Date date1 = inputDF.parse(date);
         Calendar cal = Calendar.getInstance();
         cal.setTime(date1);
         year = cal.get(Calendar.YEAR);
      }
      catch (Exception ex) {

      }

      List<RevenueQuarterlyReportDto> revenueQuarterlyReports = new ArrayList<RevenueQuarterlyReportDto>();
      for(int i = 1; i <= 12; i++) {
         List<Object[]> infos = chiTietHoaDonReportDao.getRevenueForQuarterly(String.valueOf(i), year.toString());
         if(infos != null && infos.size() > 0) {
            for(Object[] revenueForQuarterly : infos ) {
               if(revenueForQuarterly[0] != null) {
                  RevenueQuarterlyReportDto buyUserReportDto = new RevenueQuarterlyReportDto(revenueForQuarterly);
                  revenueQuarterlyReports.add(buyUserReportDto);
               }
               else {
                  RevenueQuarterlyReportDto buyUserReportDto = new RevenueQuarterlyReportDto("Tháng " + String.valueOf(i), 0.0);
                  revenueQuarterlyReports.add(buyUserReportDto);
               }
            }
         }
         else {
            RevenueQuarterlyReportDto buyUserReportDto = new RevenueQuarterlyReportDto("Tháng " + String.valueOf(i), 0.0);
            revenueQuarterlyReports.add(buyUserReportDto);
         }
      }
      return revenueQuarterlyReports;
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED)
   public List<BuyUserReportDto> getRevenueBuyOfUser(String date) {
      // show follow each user
      List<String> result = new ArrayList<String>();
      DateFormat inputDF  = new SimpleDateFormat("yyyy-MM-dd");
      Integer month = 0, year = 0 ;
      try {
         Date date1 = inputDF.parse(date);
         Calendar cal = Calendar.getInstance();
         cal.setTime(date1);
         month = cal.get(Calendar.MONTH) + 1;
         year = cal.get(Calendar.YEAR);
      }
      catch (Exception ex) {

      }

      List<BuyUserReportDto> buyUserReports = new ArrayList<BuyUserReportDto>();
      List<Object[]> buyUsers = chiTietHoaDonReportDao.getBuyOfUser(month.toString(), year.toString());
      for(Object[] buyUser : buyUsers ) {
         BuyUserReportDto buyUserReportDto = new BuyUserReportDto(buyUser);
         buyUserReports.add(buyUserReportDto);
      }

      return buyUserReports;
   }

   private List<String> getAllDateInMonth(String date) throws Exception{
      List<String> result = new ArrayList<String>();
      DateFormat inputDF  = new SimpleDateFormat("yyyy-MM-dd");
      Date date1 = inputDF.parse(date);

      Calendar cal = Calendar.getInstance();
      cal.setTime(date1);
      int month = cal.get(Calendar.MONTH) + 1;
      int day = cal.get(Calendar.DAY_OF_MONTH);
      int year = cal.get(Calendar.YEAR);
      int allDay = 0;
      switch (month) {
         case 1:
         case 3:
         case 5:
         case 7:
         case 8:
         case 10:
         case 12:
            allDay = 31;
            break;
         case 4:
         case 6:
         case 9:
         case 11:
            allDay = 30;
            break;
         case 2:
            if((year % 4 == 0 && year % 100 !=0) || year % 400 == 0) {
               allDay = 29;
            }
            else {
               allDay = 28;
            }
            break;
      }
      for(Integer i = 1; i <= allDay; i++) {
         String tem;
         if(i < 10) {
            tem = year + "-" + month + "-" + "0" + i;
         }
         else {
            tem = year + "-" + month + "-" + i;
         }
         result.add(tem);
      }

      return result;
   }
}
